package com.ajax;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.crypto.Data;
import java.io.BufferedReader;
import java.io.IOException;

/**
 * @author wzm
 * @create 2023-07-17 10:33
 * project_name:javaweb2306
 * file:AjaxServlet
 */
@WebServlet("/helloAjax")
public class AjaxServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //ajax访问服务时需要响应数据给ajax


        //获取请求过来的值
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");

        //针对通用的获取参数的方法 对于ajax不适用 get可以用 post请求必须获取请求体方式
//        String username = req.getParameter("username");
//        System.out.println("username = " + username);

        BufferedReader reader = req.getReader();
        String s = reader.readLine();
        System.out.println("s = " + s);//字符串  后期json处理

        resp.getWriter().write("hello,my ajax!!!");
        //如果返回 Student? List<Student>？ JSON
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
